package com.xuhuan.demo.aspect;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Author huan.xu
 * @Date 2023/5/4 14:44
 * @Description 切面类
 */
@Aspect
@Component
public class AopAspect {

  @AfterReturning(pointcut = "execution(* com.xuhuan.demo.aspect.TargetController.testAfterPoint(..))",
      returning = "returnValue")
  public void log(JoinPoint point, Object returnValue) {
    System.out.println("@AfterReturning：模拟日志记录功能...");
    System.out.println("@AfterReturning：目标方法为：" +
        point.getSignature().getDeclaringTypeName() +
        "." + point.getSignature().getName());
    System.out.println("@AfterReturning：参数为：" +
        Arrays.toString(point.getArgs()));
    System.out.println("@AfterReturning：返回值为：" + returnValue);
    System.out.println("@AfterReturning：被织入的目标对象为：" + point.getTarget());

    for (Map map : (List<Map>) returnValue) {
      map.put("age", map.get("id"));
    }
  }
}
